//
//  RHLocalLoadChartCell.m
//  RuiHeAppApp
//
//  Created by Yan Chen on 2024/4/8.
//

#import "RHLocalLoadChartCell.h"
#import "RHLocalBatteryStatisticsChartView.h"
#import "DevChartModel.h"
#import "DeviceChartFieldModel.h"

@interface RHLocalLoadChartCell()
@property (nonatomic ,strong) RHLocalBatteryStatisticsChartView *top_chartView;

@property (nonatomic ,strong) RHLocalBatteryStatisticsChartView *bottom_chartView;
@end
@implementation RHLocalLoadChartCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if(self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]){
        [self setupUI];
    }
    return self;
}

-(void)setTopChartModel:(DevChartModel*)topModel  bottomChartModel:(DevChartModel*)bottomModel;
{

    [_top_chartView setChartModel:topModel];
    [_bottom_chartView setChartModel:bottomModel];
    
    //计算两个图表Y轴最大值所需要的宽度,使点击上下图表的时候能对齐
    __block CGFloat y_max_width = 0;
    [topModel.selectArray enumerateObjectsUsingBlock:^(DeviceChartFieldModel*  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            double yMax = obj.y_max;
            double yMin = obj.y_min;
            NSString *maxValue = [@(yMax) twoDecimalPlacesWithoutRounding];
            NSString *minValue = [@(yMin) twoDecimalPlacesWithoutRounding];
            CGFloat max_width = [maxValue getStringWidthFont:kSYSTEMFONT(12) limitHeight:20];
            CGFloat min_width = [minValue getStringWidthFont:kSYSTEMFONT(12) limitHeight:20];
            CGFloat max_result = MAX(max_width, min_width);
            y_max_width = MAX(y_max_width, max_result);
    }];
    
    [bottomModel.selectArray enumerateObjectsUsingBlock:^(DeviceChartFieldModel*  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            double yMax = obj.y_max;
            double yMin = obj.y_min;
            NSString *maxValue = [@(yMax) twoDecimalPlacesWithoutRounding];
            NSString *minValue = [@(yMin) twoDecimalPlacesWithoutRounding];
            CGFloat max_width = [maxValue getStringWidthFont:kSYSTEMFONT(12) limitHeight:20];
            CGFloat min_width = [minValue getStringWidthFont:kSYSTEMFONT(12) limitHeight:20];
            CGFloat max_result = MAX(max_width, min_width);
            y_max_width = MAX(y_max_width, max_result);
    }];
    
    [self.top_chartView setYAxisMaxWidth:y_max_width+5];
    [self.bottom_chartView setYAxisMaxWidth:y_max_width+5];
}

- (void)setupUI{
    self.backgroundColor = UIColor.clearColor;
    [self.contentView setBackgroundColor:UIColor.clearColor];
    
    UIView *bgView = [[UIView alloc] init];
    bgView.backgroundColor = UIColor.whiteColor;
    [bgView.layer setCornerRadius:8];
    bgView.layer.masksToBounds = YES;
    [self.contentView addSubview:bgView];
    
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.insets(UIEdgeInsetsMake(0, 15, 0, 15));
    }];
    
    //------------------------
    
    
    NSInteger type = 0;
    if ([self.reuseIdentifier containsString:@"Month"]) {
        type = 1;
    }
    if ([self.reuseIdentifier containsString:@"Day"]) {
        type = 2;
    }
    if ([self.reuseIdentifier containsString:@"Year"]) {
        type = 0;
    }
    _top_chartView = [[RHLocalBatteryStatisticsChartView alloc] initWithFrame:CGRectMake(5, 5,SCREEN_WIDTH-40, (SCREEN_WIDTH-40)*0.6) type:type colorList:@[@"#57BFF5",@"#98D767",@"#98D767",@"#D177FB",@"#FDD015",@"#678EF2",@"#D177FB"]];
    _top_chartView.backgroundColor = [UIColor whiteColor];
    _top_chartView.lineChartView.xAxis.drawLabelsEnabled = NO;//不绘制X轴时间
    [bgView addSubview:_top_chartView];
    
    kWeakSelf;
    
    _bottom_chartView = [[RHLocalBatteryStatisticsChartView alloc] initWithFrame:CGRectMake(5, 5,SCREEN_WIDTH-40, (SCREEN_WIDTH-40)*0.6) type:type];
    _bottom_chartView.backgroundColor = [UIColor whiteColor];
    [bgView addSubview:_bottom_chartView];
    
  
    _top_chartView.highLightBlock = ^(ChartHighlight * highlight) {
        [weakSelf.bottom_chartView highLightPosition:highlight];
    };
    
    _bottom_chartView.highLightBlock = ^(ChartHighlight * highlight) {
        [weakSelf.top_chartView highLightPosition:highlight];
    };
    
    [_top_chartView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(5);
        make.right.mas_offset(-5);
        make.top.mas_offset(10);
        make.height.mas_equalTo(kUI_WINDOW_W*0.5+50);
    }];
    
    [_bottom_chartView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(5);
        make.right.mas_offset(-5);
        make.top.equalTo(weakSelf.top_chartView.mas_bottom).offset(0);
        make.height.mas_equalTo(kUI_WINDOW_W*0.5+50);
        make.bottom.mas_offset(-10);
    }];
}

@end
